import random

qipan = []  #棋盘
def qipan_blank():  #空白棋盘
    for i in range(6):
        list_width = []
        for j in range(8):
            list_width.append(' ' + '|')
        qipan.append(list_width)

qipan_blank()

def qipan_print():  # 打印棋盘
    print('', 1, 2, 3, 4, 5, 6, 7, 8, sep=' ')
    for i in range(6):
        print('|', end='')
        for j in range(8):
            print(qipan[i][j], end='')
        print('')
    print('——' * (9))


def eeferee():  # 判断输赢
    # 判断行
    for i in range(6):
        for j in range(8-3):
            if qipan[i][j][0] == qipan[i][j+1][0] == qipan[i][j+2][0] == qipan[i][j+3][0] and qipan[i][j][0] != ' ':
                return False
    # 判断列
    for i in range(6-3):
        for j in range(8):
            if qipan[i][j][0] == qipan[i+1][j][0] == qipan[i+2][j][0] == qipan[i+3][j][0] and qipan[i][j][0] != ' ':
                return False
    # 判断斜线
    for i in range(6 - 3):
        for j in range(8 - 3):
            if qipan[i][j][0] == qipan[i+1][j+1][0] == qipan[i+2][j+2][0] == qipan[i+3][j+3][0] and qipan[i][j][0] != ' ':
                return False
            if j >= 3:
                if qipan[i][j][0] == qipan[i + 1][j-1][0] == qipan[i+2][j-2][0] == qipan[i+3][j-3][0] and qipan[i][j][0] != ' ':
                    return False
    return True


def full():   #判断是否满了
    for i in qipan:
        for j in i:
            if j[0] == ' ':
                return True
    return False

def lara():  # 劳拉
    global qipan
    while True:
        coordinate = random.randint(0, 7)
        flag = True
        high = 0
        for i in range(5, -1, -1):
            if qipan[i][coordinate][0] == ' ':
                high = i
                break
            if i == 0 and qipan[i][coordinate][0] != ' ':
                flag = False
        if flag:
            print('>>>轮到我了,我把O棋子放在第%d列...' % (coordinate + 1))
            qipan[high][coordinate] = 'O' + '|'
            break
    qipan_print()


def user():
    global qipan
    while True:
        print(">>>轮到你了,你放X棋子,请选择列号(1-8): ", end='')
        coordinate = int(input()) - 1
        if coordinate not in range(7):
            print('输入错误的列号，请重新输入')
            continue
        flag = True
        high = 0
        for i in range(5, -1, -1):
            if qipan[i][coordinate][0] == ' ':
                high = i
                break
            if i == 0 and qipan[i][coordinate][0] != ' ':
                flag = False
                print('你输入的地方已经有棋子了，请重新输入')
        if flag:
            qipan[high][coordinate] = 'X' + '|'
            break
    qipan_print()

if __name__ == '__main__':
    print("""Hi,我是劳拉""")
    qipan_blank()
    print('开始了！这是棋盘的初始状态：')
    qipan_print()
    flag = True
    while eeferee() and full():
        lara()
        if not eeferee() and full():
            flag = False
            break
        user()
    if not full():
        print('----满了，平手')
    if flag:
        print('----你赢了')
    else:
        print('----我赢了')
